package com.you.concurrent.thread.chaindemo;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 保存processor
 */
public class SaveProcessor extends Thread implements IProcessor {

    private IProcessor nextProcessor;

    private LinkedBlockingQueue<Request> linkedBlockingQueue = new LinkedBlockingQueue();

    volatile boolean isStop = false;

    public void shutDown() {
        this.isStop = true;
    }

    public SaveProcessor() {
    }

    @Override
    public void run() {
        while (!isStop) {
            try {
                Request request = linkedBlockingQueue.take();
                //处理逻辑
                System.out.println("-----SaveProcessor------" + request.getName());
                //执行下一个processor
                if (nextProcessor != null) {
                    nextProcessor.process(request);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void process(Request request) {
        linkedBlockingQueue.add(request);
    }
}
